package com.apes.scm.rbac.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.rbac.repository.MenuRepository;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.hibernate.mapping.Map;

import javax.persistence.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 通用角色申请单明细
 * Created by lishikun on 2021/4/28 17:01
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ApplyRoleItem.CONST_TABLE_NAME)
public class ApplyRoleItem extends Model {
    public static final String CONST_TABLE_NAME = "aut_apply_role_item";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "通用角色申请单明细")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @Comment("通用角色申请单")
    @JoinColumn(name = "apply_role_id", referencedColumnName = "id", nullable = false)
    private ApplyRole applyRole; //通用角色申请单

    @ManyToOne(fetch = FetchType.LAZY)
    @Comment("菜单")
    @JoinColumn(name = "menu_id", referencedColumnName = "id", nullable = false)
    private Menu menu;

    @Comment("删除菜单")
    @Column(name = "delete_menu", nullable = false, columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    private Boolean deleteMenu;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name="operation_list", columnDefinition = "CLOB")
    @Comment("按钮权限列表")
    private String operationList;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name="reduce_operation", columnDefinition = "CLOB")
    @Comment("减少按钮功能")
    private String reduceOperation;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    @Column(name="add_operation", columnDefinition = "CLOB")
    @Comment("增加按钮功能")
    private String addOperation;


    @Transient
    private List addOperations = new ArrayList();  // 增加按钮功能

    public void setAddOperations(List<Operation> addOperations) {
        this.addOperations = addOperations;
        this.addOperation = JSON.toJSONString(addOperations);
    }

    public List getAddOperations() {
        if (this.addOperations == null && this.addOperation == null) {
            return new ArrayList();
        }
        this.addOperations = JSONArray.parseArray(this.addOperation);
        return addOperations;
    }

    @Transient
    private List reduceOperations = new ArrayList();  // 减少按钮功能

    public void setReduceOperations(List<Operation> reduceOperations) {
        this.reduceOperations = reduceOperations;
        this.reduceOperation = JSON.toJSONString(reduceOperations);
    }

    public List getReduceOperations() {
        if (this.reduceOperations == null && this.reduceOperation == null) return new ArrayList();
        this.reduceOperations = JSONArray.parseArray(this.reduceOperation);
        return JSONArray.parseArray(this.reduceOperation);
    }

    @Transient
    private Set<Operation> menuOperations = new HashSet<>();

    public void setMenuOperations(Set<Operation> menuOperations) {
        this.menuOperations = menuOperations;
    }

    public Set<Operation> getMenuOperations() {
        return menu.getOperations();
    }

    public String getAddOperation() {
        return "";
    }
    public String getReduceOperation() {
        return "";
    }
}
